package ${basePackage}.${serviceImplPackage};

import ${basePackage}.${entityPackage}.${className};
import ${basePackage}.${mapperPackage}.${className}Mapper;
import ${basePackage}.${servicePackage}.I${className}Service;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.system.entity.User;
import org.apache.shiro.SecurityUtils;
import com.jhgsys.internal.common.entity.FebsConstant;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * ${tableComment} Service实现
 *
 * @author ${author}
 * @date ${date}
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ${className}ServiceImpl extends ServiceImpl<${className}Mapper, ${className}> implements I${className}Service {

    /**
     * 查询（分页）
     * @author ${author}
     * @date ${date}
     * @param request QueryRequest
     * @param ${className?uncap_first} ${className?uncap_first}
     * @return IPage<${className}>
    */
    @Override
    public IPage<${className}> find${className}Page(QueryRequest request, ${className} ${className?uncap_first}) {
        LambdaQueryWrapper<${className}> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(${className}::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<${className}> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author ${author}
     * @date ${date}
     * @param ${className?uncap_first} ${className?uncap_first}
     * @return List<${className}>
     */
    @Override
    public List<${className}> find${className}List(${className} ${className?uncap_first}) {
	    LambdaQueryWrapper<${className}> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(${className}::getDeletedFlg, 0);
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author ${author}
    * @date ${date}
    * @param id
    * @return ${className}
    */
    @Override
    public ${className} find${className}ById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author ${author}
     * @date ${date}
     * @param ${className?uncap_first} ${className?uncap_first}
     */
    @Override
    @Transactional
    public boolean create${className}(${className} ${className?uncap_first}) {
        return this.save(${className?uncap_first});
    }

    /**
     * 修改
     * @author ${author}
     * @date ${date}
     * @param ${className?uncap_first} ${className?uncap_first}
     */
    @Override
    @Transactional
    public boolean update${className}(${className} ${className?uncap_first}) {
        return this.updateById(${className?uncap_first});
    }

    /**
     * 逻辑删除
     * @author ${author}
     * @date ${date}
     * @param ${className?uncap_first} ${className?uncap_first}
     * @param ${className?uncap_first}Ids ${className?uncap_first}Ids
     */
    @Override
    @Transactional
    public boolean delete${className}(${className} ${className?uncap_first},String ${className?uncap_first}Ids) {
        boolean result = false;
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<String> list = Arrays.asList(${className?uncap_first}Ids.split(StringPool.COMMA));

        ${className?uncap_first}.setDeletedBy(user.getUserName());
        ${className?uncap_first}.setDeletedTime(new Date());
        ${className?uncap_first}.setDeletedFlg(FebsConstant.INVALID);
        int update = this.baseMapper.update(${className?uncap_first},new LambdaQueryWrapper<${className}>().in(${className}::get${tablePkFieldName},list));
        if (update > 0 ){
            result = true;
        }
        return result;
	}

    /**
    * 物理删除
    * @author ${author}
    * @date ${date}
    * @param ${className?uncap_first}Ids ${className?uncap_first}Ids
    */
    @Override
    @Transactional
    public boolean physicallyDelete${className}(String ${className?uncap_first}Ids) {
        List<String> list = Arrays.asList(${className?uncap_first}Ids.split(StringPool.COMMA));
        LambdaQueryWrapper<${className}> wapper = new LambdaQueryWrapper<>();
        wapper.in(${className}::get${tablePkFieldName},list);
        return this.remove(wapper);
    }
}
